package com.lili.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lili.base.vo.ResultAPI;
import com.lili.mapper.SubjectMapper;
import com.lili.pojo.Subject;
import com.lili.service.SubjectService;
import com.lili.vo.request.admin.SubjectReqVO;
import com.lili.vo.response.admin.SubjectRespVO;
import com.lili.vo.response.other.KeyValue;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SubjectServiceImpl extends ServiceImpl<SubjectMapper, Subject>
        implements SubjectService {

    @Override
    public IPage<SubjectRespVO> getPageList(SubjectReqVO vo) {
        IPage<Subject> page = new Page<>(vo.getPageIndex(), vo.getPageSize());
        LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Subject::getDeleted, 0)
                .like(Strings.isNotEmpty(vo.getSubjectName()), Subject::getSubjectName, vo.getSubjectName())
                .orderByDesc(Subject::getItemOrder);
        IPage<Subject> pages = baseMapper.selectPage(page, wrapper);
        if (pages.getCurrent() < vo.getPageIndex()) {
            vo.setPageIndex(pages.getCurrent());
            page = new Page<>(vo.getPageIndex(), vo.getPageSize());
            pages = baseMapper.selectPage(page, wrapper);
        }
        IPage<SubjectRespVO> respPage = new Page<>();
        respPage.setRecords(pages.getRecords().stream().map(item -> {
                    SubjectRespVO respVO = new SubjectRespVO();
                    BeanUtils.copyProperties(item, respVO);
                    return respVO;
                }
        ).collect(Collectors.toList()));
        respPage.setPages(pages.getPages());
        respPage.setCurrent(pages.getCurrent());
        return respPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delByIds(Integer[] ids) {
        List<Subject> list = baseMapper.selectBatchIds(Arrays.asList(ids));
        list.forEach(item -> {
            item.setDeleted(1);
            this.updateById(item);
        });
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultAPI<String> editSubject(Subject subject) {
        if ((subject.getSubjectName().isEmpty()) || (subject.getItemOrder().isEmpty())) {
            return new ResultAPI<>(0, "科目名或者排序规则为空", null);
        }
        if (subject.getId() == null) {
            try {
                LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
                wrapper.eq(Subject::getSubjectName, subject.getSubjectName()).eq(Subject::getDeleted, 0);
                List<Subject> subjects = this.getBaseMapper().selectList(wrapper);
                if (!subjects.isEmpty()) {
                    return new ResultAPI<>(0, "已存在对应的科目名", null);
                }
                subject.setDeleted(0);
                this.save(subject);
                return new ResultAPI<>(200, "添加成功", null);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResultAPI<>(0, "添加失败", null);
            }
        } else {
            try {
                Subject sb = this.getSubjectById(subject.getId());
                if (!sb.getSubjectName().equals(subject.getSubjectName())) {
                    LambdaQueryWrapper<Subject> wrapper = new LambdaQueryWrapper<>();
                    wrapper.eq(Subject::getSubjectName, subject.getSubjectName()).eq(Subject::getDeleted, 0);
                    List<Subject> subjects = this.getBaseMapper().selectList(wrapper);
                    if (!subjects.isEmpty()) {
                        return new ResultAPI<>(0, "已存在对应的科目名", null);
                    }
                }
                this.updateById(subject);
                return new ResultAPI<>(200, "更新成功", null);
            } catch (Exception e) {
                e.printStackTrace();
                return new ResultAPI<>(0, "更新失败", null);
            }
        }
    }

    @Override
    public List<KeyValue> getSubjectName(String subjectName) {
        return baseMapper.selectSubjectName(subjectName);
    }

    @Override
    public List<KeyValue> selectSubjects() {
        return baseMapper.selectSubjects();
    }

    @Override
    public Subject getSubjectById(Integer id) {
        return this.getById(id);
    }
}
